#pragma once
#include"Protocol.hpp"


enum f
{
    DIV_ZERO = 1,
    MUL_ZERO,
    DEFA,
};

class ServerCal
{
public:
    ServerCal()
    {}
    ~ServerCal()
    {}

    Response CaculatorHelper(const Request& req)
    {
        Response resp(0, 0);
        switch (req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;

        case '-':
            resp.result = req.x - req.y;
            break;

        case '/':
        {
            if (req.y == 0)
                resp.code = DIV_ZERO;
            else
                resp.result = req.x / req.y;
            break;
        }

        case '%':
            if (req.y == 0)
                resp.code = MUL_ZERO;
            else
                resp.result = req.x % req.y;
            break;

        case '*':
            resp.result = req.x * req.y;
            break;

        default:
            resp.code = DEFA;
            break;

        }

        return resp;

    }

    std::string Calculator(std::string& package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if (!r)
            return "";
        Request s;
        s.Deserialize(content);
        Response resp = CaculatorHelper(s);
        std::string content2;
        resp.Serialize(&content2);
        content2 = Encode(content2);
        std::cout << "Encode" << content2 << std::endl;
        return content2;
    }
};




